﻿namespace NJQuery {
	using System;
	using System.ComponentModel;

	/// <summary>
	/// Server control used to define the properties of a <see href="http://docs.jquery.com/UI/Effects/Bounce">jQuery UI Bounce effect</see>, which bounces the element vertically or horizontally n-times.
	/// </summary>
	public class BounceEffect : EffectBase {


		#region Constants: Property defaults

		private const BounceDirection defaultDirection = BounceDirection.Up;
		private const int defaultDistance = 20;
		private const BounceMode defaultMode = BounceMode.Effect;
		private const int defaultTimes = 5;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="BounceEffect"/> class.
		/// </summary>
		public BounceEffect() : base("bounce") { }

		#endregion


		#region Properties: Initialization options

		/// <summary>
		/// The direction of the effect.
		/// This property maps directly to the "direction" option when the jQuery UI effect is initialized.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		[DefaultValue(defaultDirection), Category("Behavior")]
		public virtual BounceDirection Direction {
			get { return ViewState["Direction"] as BounceDirection? ?? defaultDirection; }
			set {
				if(!Enum.IsDefined(typeof(BounceDirection), value)) throw new ArgumentException("Value not defined.", "value");
				ViewState["Direction"] = value;
			}
		}

		/// <summary>
		/// Distance to bounce (in pixels).
		/// This property maps directly to the "distance" option when the jQuery UI effect is initialized.
		/// </summary>
		[DefaultValue(defaultDistance), Category("Behavior")]
		public virtual int Distance {
			get { return ViewState["Distance"] as int? ?? defaultDistance; }
			set { ViewState["Distance"] = value; }
		}

		/// <summary>
		/// The mode of the effect.
		/// This property maps directly to the "mode" option when the jQuery UI effect is initialized.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		public virtual BounceMode Mode {
			get { return ViewState["Mode"] as BounceMode? ?? defaultMode; }
			set {
				if(!Enum.IsDefined(typeof(BounceMode), value)) throw new ArgumentException("Value not defined.", "value");
				ViewState["Mode"] = value;
			}
		}

		/// <summary>
		/// Times to bounce.
		/// This property maps directly to the "times" option when the jQuery UI effect is initialized.
		/// </summary>
		[DefaultValue(defaultTimes), Category("Behavior")]
		public virtual int Times {
			get { return ViewState["Times"] as int? ?? defaultTimes; }
			set { ViewState["Times"] = value; }
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The <see cref="Manager"/> used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			base.Register(manager);

			manager.RegisterClientScript(ClientScript.UIBounceEffect);
		}

		/// <summary>
		/// Returns a <see cref="PropertyCollection"/> containing option values specific to the effect.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>A <see cref="PropertyCollection"/> containing the effect's option values.</returns>
		protected override PropertyCollection CreateOptionProperties() {
			PropertyCollection properties = base.CreateOptionProperties();

			BounceDirection direction = Direction;
			if(direction != defaultDirection) properties.AddEnumValue("direction", direction);

			int distance = Distance;
			if(distance != defaultDistance) properties.AddValue("distance", distance);

			BounceMode mode = Mode;
			if(mode != defaultMode) properties.AddEnumValue("mode", mode);

			int times = Times;
			if(times != defaultTimes) properties.AddValue("times", times);

			return properties;
		}

		#endregion


	}

}